package com.ht.controller;


import cn.hutool.json.JSONUtil;
import com.ht.data.Result;
import com.ht.dto.Prescription;
import com.ht.dto.User;
import com.ht.service.IPrescriptionService;
import com.ht.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 代齐园
 * @since 2022-05-25
 */
@RestController
@RequestMapping("/prescription")
@Api("关于当前用户的患者和处方信息")//Swagger2的注解
public class PrescriptionController {
    @Autowired
    private IPrescriptionService prescriptionService;

    @GetMapping("/getPrescription")
    //value为这个方法干什么用的
    @ApiOperation(value = "所有患者和处方的查询")
    //name是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParam(name = "token",value = "用户token",required = true,paramType = "header")
    public Result getPrescription(@RequestHeader("AuthToken")String token){
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);//通过请求头获取登录用户
        List<Prescription> prescription = prescriptionService.getPrescription(user.getId());
        return Result.OK().setData(prescription);
    }


}